package com.tianji.learning.controller;


import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.learning.domain.dto.LearningPlanDTO;
import com.tianji.learning.domain.po.LearningLesson;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.service.ILearningLessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学生课表 前端控制器
 * </p>
 *
 * @author 虎哥
 * @since 2024-03-11
 */
@RestController
@RequestMapping("/lessons")
@Slf4j
@Api(tags = "我的课表相关接口")
public class LearningLessonController {
    @Autowired
    private ILearningLessonService learningLessonService;



    /**
     * 我的课表分页查询排序字段
     * @param query
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "我的课表分页查询排序字段 latest_learn_time:学习时间排序，create_time:购买时间排序")
    public PageDTO<LearningLessonVO> queryMyLessons(PageQuery query){
        return learningLessonService.queryMyLessons(query);
    }

    /**
     * 查询最近学习的课程
     * @return
     */
    @GetMapping("/now")
    @ApiOperation(value = "查询最近学习的课程")
    public LearningLessonVO queryMyCurrentLesson(){

        return learningLessonService.queryMyCurrentLesson();
    }


    @DeleteMapping("/{courseId}")
    @ApiOperation(value = "删除课表")
    public void deleteLesson(@PathVariable(name = "courseId") Long courseId){
        learningLessonService.deleteLesson(courseId);
    }

    /**
     * 检查课程是否有效
     * @param courseId
     * @return
     */
    @GetMapping("/{courseId}/valid")
    @ApiOperation(value = "检查课程是否有效")
    public Long isLessonValid(@PathVariable(name ="courseId") Long courseId){
        return learningLessonService.isLessonValid(courseId);
    }

    /**
     * 查看用户指定课程状态
     * @param courseId
     * @return
     */
    @GetMapping("/{courseId}")
    @ApiOperation(value = "查看用户指定课程状态")
    LearningLesson getLessonStatus(@PathVariable(name = "courseId") Long courseId){
        return learningLessonService.getLessonStatus(courseId);
    }

    /**
     * 统计课程的学习人数
     * @param courseId
     * @return
     */
    @GetMapping("/{courseId}/count")
    @ApiOperation(value = "统计课程的学习人数")
    Integer countLearingLessonByCourse(@PathVariable(name = "courseId") Long courseId){
        return learningLessonService.countLearingLessonByCourse(courseId);
    }

    /**
     * 创建学习计划
     * @param learningPlanDTO
     */
    @PostMapping("/plans")
    @ApiOperation(value = "创建学习计划")
    public void createPlan(@RequestBody LearningPlanDTO learningPlanDTO){
        learningLessonService.createPlan(learningPlanDTO);
    }

    /**
     * 定时任务 没隔15s查询课表课程是否过期 如果过期则修改课表状态
     */
    @Scheduled(cron = "*/15 * * * * ? ")
    public void springTaskExpireTime(){
    learningLessonService.springTaskExpireTime();
    }

    /**
     * 查询我本周的学习计划
     * @param pageQuery
     * @return
     */
    @GetMapping("/plans")
    @ApiOperation(value = "查询我本周的学习计划")
    public LearningPlanPageVO queryMyPlans(PageQuery pageQuery){
        return learningLessonService.queryMyPlans(pageQuery);
    }
}
